<?php
// +----------------------------------------------------------------------
// | 商品分类
// +----------------------------------------------------------------------
// | Author: myh
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\lib\oss\Oss;
use app\admin\service\GoodsCategoryService;

class GoodsCategory extends AdminController
{
    //列表
    public function list(){
        return GoodsCategoryService::listTree($this->request->get());
    }

    //新增
    public function add(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'category_name'  => 'require',
        ];
        //提示信息
        $message = [
            'category_name.require' => '分类名称不能为空!'
        ];

        $this->validate($data, $validate, $message);
        return GoodsCategoryService::save($data);
    }

    //更新
    public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'category_name'  => 'require'
        ];
        //提示信息
        $message = [
            'id.require' => '请选择要更新的数据!',
            'category_name.require' => '分类名称不能为空!'
        ];

        $this->validate($data, $validate, $message);
        return GoodsCategoryService::update($data);
    }

    //删除
    public function delete(){
        $ids = $this->request->get("ids");
        if(empty($ids)){
            return failure(config('error.er5')['code'],"请选择要删除的数据");
        }

        return GoodsCategoryService::destroy($ids);
    }

    //上传图片
    public function upload(){
        $file = $this->request->file('file');
        $pathName = $file->getPathname();
        $originalName = $file->getOriginalExtension();

        $object = "goods/category/".$file->hash('md5').".".$originalName;
        return success(Oss::uploadFile($object,$pathName));
    }

}
